

import java.util.Scanner;

public class LibraryManagementSystem {
    public static void main(String[] args) {
        //初始化信息
            //初始化菜单信息：
        String [] dishName = new String[] {"红烧带鱼","鱼香肉丝","时令鲜蔬","麻婆豆腐"};    //菜品名称
        double[] prices = {38.0,20.0,10.0,10.0};     //菜品单价
        int[] praiseNums = new int[4];      //菜品点赞数

            //初始化数组及其订单信息：
        String[] names = new String[4]; //保存订餐人的姓名
        String[] dishNames = new String[4]; // 保存要点的菜
        int[] megs = new int[4];// 保存要定的菜的份数
        int[] times = new int[4];//保存要派送的时间
        String[] addresses = new String[4];//保存派送地址
        int[] states = new  int[4]; //保存订单状态。 0 表示已经预定  1 表示订单已经完成
        double[] sumPrices = new double[4];//保存订单的总金额

        //初始化第一条订单信息
        names[0] = "张晴";
        dishNames[0] = "红烧带鱼";
        megs[0] = 2;
        times[0] = 12;
        addresses[0] = "天成路207号";
        states[0] = 1;
        sumPrices[0] = 76.0;

        //初始化第二条订单信息
        names[1] = "张晴";
        dishNames[1] = "鱼香肉丝";
        megs[1] = 2;
        times[1] = 18;
        addresses[1] = "天成路207号";
        states[1] = 0;
        sumPrices[1] = 45.0;

        int num = 1; //判断是否再次回到菜单
        boolean isExit = false;  //标记：trun为退出系统 f为返回菜单

        do {
            System.out.println("*****************欢迎使用“吃货联盟订餐系统”**********************");
            System.out.println("************************************************************");
            System.out.println("1.我要订餐");
            System.out.println("2.查看餐袋");
            System.out.println("3.签收订单");
            System.out.println("4.删除订单");
            System.out.println("5.我要点赞");
            System.out.println("6.退出系统");
            System.out.println("************************************************************");

            System.out.print("请选择：");
            Scanner sc = new Scanner(System.in);
             num = sc.nextInt();
            switch (num){
                case 1:
                    //1.我要订餐
                    System.out.println("*****我要订餐*****");
                    boolean isAdd = false; //标记：true为可以点餐 f为不能点单
                    for (int i = 0; i < names.length ; i++) {
                        if (names[i] == null) {
                            isAdd = true;
                            System.out.print("请输入订餐人的姓名：");
                            String name = sc.next();
                            System.out.println("序号\t\t"+"菜名\t\t"+"单价\t\t"+"点赞数");
                            for (int j = 0; j < dishName.length; j++) {
                                System.out.println((j+1) + "\t\t" + dishName[j] + "\t\t" + prices[j] +"元" + "\t\t" + praiseNums[j] );
                            }
                            System.out.print("请选择你要点的菜品编号：");
                            int chooseDish = sc.nextInt();
                            String dishMeg = dishNames[chooseDish-1]; // 用户选择的菜品编号与数组下标差一
                            System.out.print("请选择你要点的菜品的份数：");
                            int number = sc.nextInt();
                            System.out.print("请输入送餐时间（送餐时间是10点至20点整点送餐）：");
                            int time = sc.nextInt();
                            while (time <10 || time >20){
                                System.out.println("抱歉！！你输入的时间信息有误，请重新输入！");
                                 time = sc.nextInt();
                            }
                            System.out.println("请输入送餐地址：");
                            String address = sc.next();
                            System.out.println("订餐成功！！");
                            System.out.println("您定的是:" +dishMeg + number+"份" );
                            System.out.println("送餐时间是：" + time +"点");
                            double sumPrice = prices[chooseDish-1] * number;
                            double sendMoney = (sumPrice>50)?0:6;
                            System.out.println("餐费：" + sumPrice +"元，送餐费：" +sendMoney +"元，总计：" + (sumPrice+sendMoney)+"元。");

                            //添加数据
                            names[i] = name;
                            dishNames[i] = dishMeg;
                            megs[i] = number;
                            times[i] = time;

                            addresses[i] = address;
                            sumPrices[i] = sumPrice + sendMoney;

                            break;
                        }
                    }
                    if (!isAdd){
                        System.out.println("抱歉餐袋已经满了不能点餐！");
                    }
                    break;
                case  2:
                    //2.查看餐袋
                    System.out.println("***查看餐袋***");
                    System.out.println("序号\t\t" + "订餐人\t\t" + "餐品信息\t\t" + "送餐时间\t\t" + "送餐地址\t\t" + "总金额\t\t " + "订单状态\t\t");
                    for (int i = 0; i < names.length  ; i++) {
                        if (names[i] != null) {
                            String state =(states[i] == 0)?"已预订":"已完成";
                            /*String[] names = new String[4]; //保存订餐人的姓名
                            String[] dishNames = new String[4]; // 保存要点的菜
                            int[] megs = new int[4];// 保存要定的菜的份数
                            int[] times = new int[4];//保存要派送的时间
                            String[] addresses = new String[4];//保存派送地址
                            int[] states = new  int[4]; //保存订单状态。 0 表示已经预定  1 表示订单已经完成
                            double[] sumPrices = new double[4];//保存订单的总金额*/
                            System.out.println((i + 1) + "\t\t" + names[i] + "\t\t" + (dishNames[i] + megs[i]) + "份"
                                    + "\t\t" + times[i] + "\t\t" + addresses[i] + "\t\t" + sumPrices[i] + "元" + "\t\t" + state);
                        }
                    }

                    break;
                case 3:
                    //3.签收订单
                    System.out.println("****签收订单****");
                    boolean isSignFind = false;   //标记：f为不能签收订单， t为订单可以签收。
                    System.out.println("请选择要签收的订单序号：");
                    int sign = sc.nextInt();
                    for (int i = 0; i < names.length ; i++) {
                        if ( sign == (i+1) && states[i] == 0) {  //可以签收条件：将要签收的订单所在数组不为空，并且用户输入的订单序号
                            // 与数组所对应的下标差1，订单状态还要在已预定（0）的状态
                            isSignFind = true;  //更改标记为已经签收
                            states[i] = 1;
                            System.out.println("订单签收成功！！！");

                        }else if( sign == (i+1) && states[i] == 1){ // 不可以签收：无法签收一个已经签收了的订单
                            System.out.println("抱歉！！该订单已经签收，不能再次签收。");
                            isSignFind = true;
                        }
                    }
                    if (!isSignFind){
                        System.out.println("抱歉！！找不到你所输入的订单。");
                    }


                    break;
                case 4:
                    System.out.println("***删除订单***");
                    boolean isDelFind = false;
                    System.out.print("请输入要删除的订单序号：");
                    int delID = sc.nextInt();
                    for (int i = 0; i < names.length; i++) {
                        // 根据状态值判断能不能删除，只有已完成的才能删除
                        if (names[i] != null && states[i] == 1 && delID == i + 1) {
                            isDelFind = true;
                            for (int j = delID - 1; j < names.length - 1; j++) {
                                names[j] = names[j + 1];
                                dishNames[j] = dishNames[j + 1];
                                megs[j] = megs[j + 1];
                                times[j] = times[j + 1];
                                addresses[j] = addresses[j + 1];
                                states[j] = states[j + 1];
                                sumPrices[j] = sumPrices[j + 1];
                            }
                            // 将最后一位清空
                            names[names.length - 1] = null;
                            dishNames[names.length - 1] = null;
                            megs[names.length - 1] = 0;
                            times[names.length - 1] = 0;
                            addresses[names.length - 1] = null;
                            states[names.length - 1] = 0;
                            sumPrices[names.length - 1] = 0;
                            System.out.println("删除订单成功！");
                            break;
                        } else if (names[i] != null && states[i] == 0 && delID == i + 1) {
                            isDelFind = true;
                            System.out.println("您选择的订单未签收，不能删除！");
                            break;
                        }
                    } // 未找到的订单不能删除
                    if (!isDelFind) {
                        System.out.println("您要删除的订单不存在！");
                    }
                    break;
                case  5:
                    System.out.println("***我要点赞***");
                    // 显示菜品信息
                    System.out.println("序号\t\t菜名\t\t单价");
                    for (int i = 0; i < dishNames.length; i++) {
                        String priaiseNum = (praiseNums[i] > 0) ? praiseNums[i] + "赞" : "";
                        System.out.println((i + 1) + "\t" + dishName[i] + "\t" + prices[i] + "元" + "\t" + priaiseNum);
                    }
                    System.out.print("请选择您要点赞的菜品序号：");
                    int number = sc.nextInt();
                    praiseNums[number - 1]++;
                    System.out.println("点赞成功");
                    break;
                case 6:
                    break;
            }
            if (!isExit){
                System.out.println("输入0返回菜单,否则退出系统。");
                num = sc.nextInt();
            }else {
                break;
            }

        } while (num == 0);





    }
}
